package com.wyl.auth.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wyl.auth.dto.SysPermissionDto;
import com.wyl.auth.entity.SysPermission;
import com.wyl.auth.mapper.SysPermissionMapper;
import com.wyl.auth.req.SysPermissionReq;
import com.wyl.auth.service.SysPermissionService;
import com.wyl.auth.util.BeanAwareUtil;
import com.wyl.auth.vo.SysPermissionVo;
import com.wyl.common.CommonPage;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Description 系统权限
* @Author WuYiLong
* @Date 2025-04-04
*/
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Override
    public void save(SysPermissionDto dto) {
        if(dto.getId() != null) {
            SysPermission sysPermission = getById(dto.getId());
            BeanAwareUtil.copyProperties(dto, sysPermission);
            sysPermission.setUpdateTime(LocalDateTime.now());
            sysPermission.setUpdateUserId(StpUtil.getLoginIdAsLong());
            sysPermission.updateById();
        }else{
            SysPermission sysPermission = new SysPermission();
            BeanAwareUtil.copyProperties(dto, sysPermission);
            sysPermission.setCreateTime(LocalDateTime.now());
            sysPermission.setCreateUserId(StpUtil.getLoginIdAsLong());
            save(sysPermission);
        }
    }

    @Override
    public CommonPage<SysPermissionVo> page(SysPermissionReq req) {
        Page<SysPermission> page = new Page<>(req.getCurrentPage(), req.getPageSize());
        LambdaQueryWrapper<SysPermission> wrapper = Wrappers.lambdaQuery(SysPermission.class);
        Page<SysPermission> pageResult = page(page, wrapper);
        List<SysPermissionVo> sysPermissionVos = pageResult.getRecords().stream().map(m -> {
            SysPermissionVo sysPermissionVo = new SysPermissionVo();
            BeanAwareUtil.copyProperties(m, sysPermissionVo);
            return sysPermissionVo;
        }).collect(Collectors.toList());

        CommonPage<SysPermissionVo> commonPage = new CommonPage<>(req.getCurrentPage(), req.getPageSize());
        commonPage.setList(sysPermissionVos);
        commonPage.setTotal((int)pageResult.getTotal());
        return null;
    }

    @Override
    public void deleteByIds(List<Long> ids) {

    }
}
